package com.example.musicserver.interceptor;

import com.example.musicserver.config.ResponseBodyMessage;
import com.example.musicserver.constant.Constants;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Hua YY
 * Date: 2025-01-24
 * Time: 15:10
 */
@Slf4j
//@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response, Object handler) throws Exception {
        HttpSession httpSession = request.getSession(false);
        if(httpSession == null || httpSession.getAttribute(Constants.USERINFO_SESSION_KEY) == null) {
            log.info("请求被拦截");
            response.setStatus(401);
            log.info("返回状态码成功");
            return false;
        }
        return true;
    }
}
